import 'dart:ffi';

import 'package:HnXG/Request/Request3.dart';
import 'package:HnXG/Request/Request4.dart';
import 'package:HnXG/Request/Request5.dart';
import 'package:HnXG/Request/Request6.dart';
import 'package:HnXG/Request/Request7.dart';
import 'package:HnXG/Request/Request8.dart';
import 'package:HnXG/Request/TaskAgvRequest.dart';
import 'package:HnXG/Request/UserLoginRequest%20.dart';
import 'package:HnXG/snackbar.dart';
import 'package:dio/dio.dart';
import 'package:flutter/src/widgets/framework.dart';

class Api {
  // 在_dio配置中添加通用请求头
  static final Dio _dio = Dio(BaseOptions(
    headers: {'Content-Type': 'application/json', 'Accept': 'application/json'},
    baseUrl: 'http://192.168.31.12:1022/api/',
    connectTimeout: const Duration(seconds: 5),
    receiveTimeout: const Duration(seconds: 3),
  ));//http://10.10.1.15/r

  static String jwt="";
 //创建去产线任务
static Future<Map<String, dynamic>> CreateFromCachaTaskOrder() async {
              try {
      final response = await  _dio .post('BaseService/CreateFromCachaTaskOrder',       
       options: Options(headers: {
          'Authorization': 'bearer ${Api.jwt}'  // 添加Bearer Token
        }),);
      print("S");
      
      return {
        'msg': response.data['msg'],
        'dto': response.data['dto'],
        'state': response.data['state'],
      };
    } on DioException catch (e) {
 
      return {

       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
        }
  //创建去缓存任务
static Future<Map<String, dynamic>> CreateToCacheTaskOrder(Request8
        Request) async {
              try {
      final response = await  _dio .post('BaseService/CreateToCacheTaskOrder',data: Request,        
          options: Options(headers: {
          'Authorization': 'bearer ${Api.jwt}'  // 添加Bearer Token
        }),);
  
      
      return {
        'msg': response.data['msg'],
        'dto': response.data['dto'],
        'state': response.data['state'],
      };
    } on DioException catch (e) {
 
      return {

       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
        }

//  agv任务
  static Future<Map<String, dynamic>> CreateTaskOrder(TaskAgvRequest
        Request) async {
       
    try {
      final response = await  _dio .post(
        'BaseService/CreateTaskOrder',
        data: Request,
        options: Options(headers: {
          'Authorization': 'bearer ${Api.jwt}'  // 添加Bearer Token
        }),
      );
  
      
      return {
        'msg': response.data['msg'],
        'dto': response.data['dto'],
        'state': response.data['state'],
      };
    } on DioException catch (e) {
 
      return {

       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
  }
// 队列视图
  static Future<Map<String, dynamic>> GetView_TaskOrderInfo() async {
       
    try {
      final response = await  _dio .post('BaseService/GetView_TaskOrderInfo');
    print("22");
      print(response.data['dto']);
      return {
        'msg': response.data['msg'],
        'dto': response.data['dto'],
        'state': response.data['state'],
      };
    } on DioException catch (e) {
 
      return {

       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
  }

// 用户登录
  static Future<Map<String, dynamic>> loginRequest(
      UserLoginRequest user) async {
    try {
      final response = await _dio.post(
        'User/LoginUser',
        data: user,
      );
      jwt = response.data['dto'];  // 现在可以正常访问
      print(jwt);
      return {
        'state': response.data['state'], //我想打印出来
        'dto': response.data['dto'],
        'msg': response.data['msg'],
      };
    } on DioException catch (e) {
      print( "cuowu ${e.message}");
      return {
       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
  }

//获取任务列表
  static Future<Map<String, dynamic>> GetView_TaskOrder() async {
   
      try {
      final response = await _dio.post(
        'BaseService/GetView_TaskOrder',
                options: Options(headers: {
          'Authorization': 'bearer ${Api.jwt}'  // 添加Bearer Token
        }),

      );

    
      return {
        'state': response.data['state'], //我想打印出来
        'dto': response.data['dto'],
        'msg': response.data['msg'],
      };
    } on DioException catch (e) {
      return {
       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
    
  }

//获取位置状态
   static Future<Map<String, dynamic>> GetView_PositionState(Request3 Request) async {
   
      try {
      final response = await _dio.post(
        'BaseService/GetView_PositionState',data: Request
      );

    
      return {
        'state': response.data['state'], 
        'dto': response.data['dto'],
        'msg': response.data['msg'],
      };
    } on DioException catch (e) {
      return {
       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
    
  }

//取消任务

  static Future<Map<String, dynamic>> CancelTaskOrder(Request4
        Request) async {
       
    try {
      final response = await  _dio .post('BaseService/CancelTaskOrder', data: Request);
  
      
      return {
        'msg': response.data['msg'],
        'dto': response.data['dto'],
        'state': response.data['state'],
      };
    } on DioException catch (e) {
 
      return {

       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
  }

//取消队列
  static Future<Map<String, dynamic>> DeleteTaskInfo(String taskinfo_Id) async {
       
    try {
      final response = await  _dio.post('BaseService/DeleteTaskInfo?taskinfo_Id=${taskinfo_Id}',
        options: Options(headers: {
          'Authorization': 'bearer ${Api.jwt}'
        }));
  
      
      return {
        'msg': response.data['msg'],
        'dto': response.data['dto'],
        'state': response.data['state'],
      };
    } on DioException catch (e) {
 
      return {

       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
  }

//测试接口
  static Future<Map<String, dynamic>> testRequest() async {
    try {
      final url = _dio.options.baseUrl + 'BaseService/test';
       print(url);
      final response = await _dio
          .post('BaseService/test'); //这里的地址没有问题  我用postman测过了  但是在flutter中就不行了
      return {
        'state': response.data['state'], //我想打印出来
        'msg': response.data['msg'],
        'dto': response.data['dto'],
      };
    } on DioException catch (e) {
      return {
        'error': '请求失败',
        'message': e.message,
        'statusCode': e.response?.statusCode
      };
    }
  }


//添加物料
  static Future<Map<String, dynamic>> CreateMaterialList(Request5 Request ) async {

    try {
      final response = await  _dio .post('BaseService/CreateMaterialList', data: Request);
  
      
      return {
        'msg': response.data['msg'],
        'dto': response.data['dto'],
        'state': response.data['state'],
      };
    } on DioException catch (e) {
 
      return {

       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
  }

//获取物料
  static Future<Map<String, dynamic>> GetMaterialList(Request6 Request) async {
    
    try {
      final response = await  _dio .post('BaseService/GetMaterialList', data: Request);
  
      
      return {
        'msg': response.data['msg'],
        'dto': response.data['dto'],
        'state': response.data['state'],
      };
    } on DioException catch (e) {
 
      return {

       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }
  }

//搜索物料
  static Future<Map<String, dynamic>>  SearchMaterialList(Request7 Request) async {
      try {
      final response = await  _dio .post('BaseService/SearchMaterialList', data: Request);
  
      print(response.data['dto']);
      return {
        'msg': response.data['msg'],
        'dto': response.data['dto'],
        'state': response.data['state'],
      };
    } on DioException catch (e) {
 
      return {

       'msg': e.message,
        'dto': null,
        'state': false,
      };
    }

  }
}